home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / BackSpaceViews / WanderingPolygonView.BackModule / WanderingPolygonViewPart.m < prev   
Text File  |  1995-06-12  |  3KB  |  191 lines

  1.  
  2. #import    <stdlib.h>
  3. #import    <libc.h>
  4. #import    <math.h>
  5. #import    <time.h>
  6. #import <sys/time.h>
  7. #import    <appkit/Application.h>
  8. #import    <appkit/Slider.h>
  9. #import    <appkit/Button.h>
  10. #import    <appkit/NXImage.h>
  11. #import    <dpsclient/wraps.h>
  12. #import    "WanderingPolygonViewPart.h"
  13.  
  14. @implementation WanderingPolygonView
  15. extern float frandom();
  16. extern float randBetween(float a, float b);
  17.  
  18. -randPoint:(NXPoint *)thePoint;
  19. {
  20.     thePoint->x =randBetween(0, bounds.size.width);
  21.     thePoint->y =randBetween(0, bounds.size.height);
  22.     return self;
  23. }
  24.  
  25. -randMovement:(NXPoint *)thePoint;
  26. {
  27.     thePoint->x =randBetween(-3,3);
  28.     thePoint->y =randBetween(-3, 3);
  29.     return self;
  30. }
  31.  
  32. -reset;
  33. {
  34.     int x;
  35.     for (x=0; x<NUMPOINTS;x++) {
  36.         [self randPoint:&p[x]];
  37.         [self randMovement:&m[x]];
  38.     }
  39.     NUMPOINTS = floor(randBetween(MINPOINTS, MAXPOINTS));
  40.     return self;
  41. }
  42.  
  43. - ( const char * ) windowTitle
  44. {
  45.     return ( const char * ) "Wandering Polygon";
  46. }
  47.  
  48. /**********************************************************************/
  49.  
  50. - newWindow
  51. {
  52.     [self reset];
  53.     return self;
  54. }
  55.  
  56. /**********************************************************************/ 
  57.  
  58. - initFrame : ( const NXRect * ) frameRect
  59. {
  60.     [ super initFrame : frameRect ];
  61.     [ self setOpaque : YES ];    
  62.     [self reset];
  63.     r=0;
  64.     g=0;
  65.     b=0;
  66.     return self;
  67. }
  68.  
  69. /**********************************************************************/
  70.  
  71. - sizeTo : ( NXCoord ) width : ( NXCoord ) height
  72. {
  73.     [ super sizeTo : width : height ];
  74.     
  75.     [ self reset ];
  76.     
  77.     return self;
  78. }
  79.  
  80. /**********************************************************************/
  81.  
  82. - drawSelf : ( NXRect * ) rects : ( int ) count
  83. {
  84.     if ( !rects || !count )
  85.         return self;
  86.         
  87.     PSsetgray( NX_BLACK );
  88.     
  89.     NXRectFill( rects );
  90.     steps = 0;
  91.     return self;
  92. }
  93.  
  94.  
  95. /**********************************************************************/
  96. void NXPutPointInRect(NXPoint *aPoint, const NXRect *aRect, NXCoord inset)
  97. {
  98.     NXRect bRect;
  99.     bRect = *aRect;
  100.   
  101.     NXInsetRect(&bRect, inset, inset);
  102.  
  103.     if (aPoint->x  < bRect.origin.x)        aPoint->x = bRect.origin.x;
  104.     if (aPoint->y < bRect.origin.y) aPoint->y = bRect.origin.y;
  105.  
  106.     if (aPoint->y > bRect.origin.y+bRect.size.height)
  107.         aPoint->y = bRect.origin.y+bRect.size.height;
  108.  
  109.     if (aPoint->x > bRect.origin.x+bRect.size.width)
  110.         aPoint->x = bRect.origin.x+bRect.size.width;
  111.  
  112.     return;
  113. }
  114.  
  115. -incColor;
  116. {
  117.     steps++;
  118.     r = fabs(sin((steps+417)/217.0));
  119.     g = fabs(sin((steps+273)/113.0));
  120.     b = fabs(sin((steps+913)/329.0));
  121. //    printf("%f %f %f \n",r,g,b);
  122.     return self;
  123. }
  124.  
  125. -incTriangle;
  126. {
  127.     int x;
  128.  
  129.     for (x=0; x<NUMPOINTS;x++) {
  130.         p[x].x = p[x].x+m[x].x;
  131.         p[x].y = p[x].y+m[x].y;
  132.         if (!NXPointInRect(&p[x], &bounds)) {
  133.             NXPutPointInRect(&p[x],&bounds,1);
  134.             [self randMovement:&m[x]];
  135.         }
  136.     }
  137.     [self incColor];
  138.     return self;
  139. }
  140.  
  141. -drawTriangle;
  142. {
  143.     int x;
  144.     PSnewpath( );    
  145.     PSsetlinewidth(5.0);
  146.     PSsetlinejoin(2);
  147.     PSsetrgbcolor(1-r,1-g, 1-b);
  148.     PSmoveto(p[0].x,p[0].y);
  149.     for (x=1; x<NUMPOINTS;x++) {
  150.         PSlineto(p[x].x,p[x].y);
  151.     }
  152.     PSlineto(p[0].x,p[0].y);
  153.     PSstroke( );
  154.     return self;
  155. }
  156.  
  157. -blackTriangle;
  158. {
  159.     int x;
  160.     PSnewpath( );    
  161.     PSsetrgbcolor(0, 0, 0);    
  162.     PSmoveto(p[0].x,p[0].y);
  163.     for (x=1; x<NUMPOINTS;x++) {
  164.         PSlineto(p[x].x,p[x].y);
  165.     }
  166.     PSlineto(p[0].x,p[0].y);
  167.     PSeofill( );
  168.     [self reset];
  169.     return self;
  170. }
  171.  
  172. - oneStep
  173. {
  174.     int x;
  175.     if (steps ==10000) {
  176.         steps =0;
  177.         for (x=0 ; x<1000 ; x++) {
  178.             [self blackTriangle];
  179.             [self incTriangle];
  180.         }
  181.     }
  182.     [self drawTriangle];
  183.     [self incTriangle];
  184.     return self;
  185. }
  186.  
  187.  
  188. /**********************************************************************/
  189.  
  190. @end
  191.